<?php
/**
 * Model Extensions
 * @author	Jeremy Lindblom
 */

// Loadable Model
class ML extends M{const ALL=-1;function load($c,$i){return($i==ML::ALL?db\s($c):db\r($c,$i));}function loaded(){return(bool)$this->id;}}

// Loadable Relationships Model
class MLR extends ML{function related($k){M::factory($k);$c=get_class($t=$this);$m='db\\'.@$t->relationships[$k];return$m($c,$k,$t);}}

// Savable Model
class MS extends MLR{function save(){$c=get_class($t=$this);$r=($i=&$t->id)?db\u($c,$t,$i):db\i($c,$t);$i=$i?:db\c()->insert_id;return$r;}}

// Savable Relationships Model
class MSR extends MS{function add(M$o){list($g,$c,$f)=db\g($t=$this,$o);return@db\i($g,array($c.'_id'=>$t->id,$f.'_id'=>$o->id));}}

// Deletable Model
class MD extends MSR{function delete(){return db\d(get_class($this),$this->id);}function delete_all(){return db\d(get_class($this),0);}}

// Deletable Relationships Model
class MDR extends MD{function remove(M$o){list($g,$c,$f)=db\g($t=$this,$o);return@db\d($g,"{$c}_id={$t->id} AND {$f}_id={$o->id}");}}